<include file="Public/head"/>
<link type="text/css" rel="stylesheet" href="{:get_style_cdnurl()}paimai/yunying/plugin/select2/select2.css" />
<script src="{:get_style_cdnurl()}paimai/yunying/plugin/select2/select2.js" type="text/javascript"></script>
<script src="{:get_style_cdnurl()}paimai/yunying/plugin/ztree/jquery.ztree.core-3.5.js" type="text/javascript"></script>
<script src="{:get_style_cdnurl()}paimai/yunying/plugin/ztree/jquery.ztree.exedit-3.5.js" type="text/javascript"></script>
<script type="text/javascript">
    var setting = {
        view: {
            dblClickExpand: dblClickExpand,
            addHoverDom: addHoverDom,
            removeHoverDom: removeHoverDom
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: zTreeOnClick1
        }
    };

    var setting1 = {
        view: {
            // addHoverDom: addHoverDom,
            // removeHoverDom: removeHoverDom,
        },

        data: {
            simpleData: {
                enable: true
            }
        },
        callback:{
            onClick: zTreeOnClick
        }
    };

    var zNodes =[
        <?php foreach($list as  $item):?>
        {id:<?php echo $item['id'];?>, pId:<?php echo $item['pid'];?>, name:"<?php echo $item['name'];?>",<?php echo $item['id']==1 ? "open: true" : '' ;?>},
        <?php endforeach;?>
    ];

    function zTreeOnClick1(event, treeId, treeNode)
    {
        $.post("/Depart/getDepartMember",{"id":treeNode.id},function(data){
            if(data.code!=200){
                //TODO:右边框加载中
            }
            //<li><span>谭小斌</span><span>18692277486</span></li>
            var htmlstr = '';
            $.each(data.data,function(i,item){
                if (item.is_head == 1) {
                    item.is_head_name = "是";
                }else{
                    item.is_head_name = "否";
                }
                htmlstr += "<li><span>"+ item.real_name + "</span><span>" + item.mobile + "</span><span>"+ item.is_head_name+"</span></li>";
            });
            $("#departMember").html(htmlstr);
        },"json");
    }

    function addHoverDom(treeId, treeNode) {
        if (treeNode.parentNode && treeNode.parentNode.id!=1) return;
        var aObj = $("#" + treeNode.tId + "_a");
        if ($("#diyBtn1_"+treeNode.id).length>0) return;
        if ($("#diyBtn2_"+treeNode.id).length>0) return;
        var editStr = "<a id='diyBtn1_" +treeNode.id+ "' onclick='editNode(\"" + treeNode.tId + "\");return false;' style='margin:2px 0 0 5px;'>编辑</a>" +
                "<a id='diyBtn2_" +treeNode.id+ "' onclick='deleteNode(\"" + treeNode.tId + "\");return false;' style='margin:2px 0 0 5px;'>删除</a>";
        aObj.append(editStr);
    }
    function removeHoverDom(treeId, treeNode) {
        if (treeNode.parentNode && treeNode.parentNode.id!=1) return;
        $("#diyBtn1_"+treeNode.id).unbind().remove();
        $("#diyBtn2_"+treeNode.id).unbind().remove();

    }

    function dblClickExpand(treeId, treeNode) {
        return treeNode.level > 0;
    }

    function zTreeOnClick( event, treeId, treeNode){
        // console.log(treeNode);
        $("#parent_id").val(treeNode.id);
        if (treeNode.id == 1) {
            $(".branch_title").show();
        }else{
            $(".branch_title").hide();
        }
        $("#onadd").val(treeNode.name);
        $(".deparHover div.zTreeDemoBackground").fadeOut();
    }

    function editNode(tId){
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var node = treeObj.getNodeByTId(tId);
        $.post("/Depart/getDepart",{"id":node.id},function(data){
            if(data.code!=200){
                shibai_alert(data.msg, 1000);
                return false;
            }
            $("#depart_id").val(node.id);
            $("#onadd").val(data.data.parent_name).attr('data-id',data.data.pid);
            $("#parent_id").val(data.data.pid);
            if (data.data.pid == 1) {
                $(".branch_title").show();
                if (data.data.is_branch_department == 1) {
                    $('input[name=is_branch_department]').get(0).checked = true;  
                }else if(data.data.is_branch_department == 0){
                    $('input[name=is_branch_department]').get(1).checked = true;  
                }
            }else{
                $(".branch_title").hide();
            }
            var optstr = '';
            if (data.data.leader && data.data.leader.length > 0) {
                $.each(data.data.leader, function (k, val) {
                    optstr += "<option value='" + val.emp_id + "'>" + val.real_name + "</option>";
                })
            }
            
            $("#inCharge").html(optstr);
            if (data.data.leader && data.data.leader.length > 0) {
                $.each(data.data.leader, function (k, val) {
                    if (val.emp_id == data.data.leaderhead) {
                        $("#inCharge option[value='"+data.data.leaderhead+"']").attr("selected", true);
                    };
                })
            };
            
            // $("#inCharge").val(data.data.leader).attr('data-id', data.data.emp_id);
            // $("#inCharge").val(data.data.leaderheadname);
            $("#inCharge").select2();
            $("#responsible_person").show();
            $("#depart_name").val(data.data.name);
            $(".deparHover h4").html("编辑部门");
            $("#depart_form").attr("action","/Depart/upDepart");
            $(".deparHover").fadeIn();
            $(".yunBei").fadeIn();
        },"json");
    }

    function deleteNode(tId){
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var node = treeObj.getNodeByTId(tId);
        if(!confirm("一旦删除无法恢复,是否确认删除部门" + node.name + "?")){
            return false;
        }
        $.post("/Depart/delDepart",{"id":node.id},function(data){
            if(data.code!=200){
                shibai_alert(data.msg, 1000);
                return false;
            }
            //删除成功
            btn_alert('删除成功!',1000);
            treeObj.removeNode(node);
        },"json");
    }

    $(document).ready(function(){
        $.fn.zTree.init($("#tree"), setting, zNodes);
        $.fn.zTree.init($("#tree1"), setting1, zNodes);

        $(".yunBei").unbind();
        $("#inCharge").select2();

        $(".department .addTo").click(function(){
            $(".deparHover h4").html("添加部门");
            $("#onadd").val("").attr('data-id',"");
            $("#inCharge").val("").attr('data-id', "");
            $("#depart_name").val("");
            $("#depart_form").attr("action","/Depart/addDepart");
            $(".deparHover").fadeIn();
            $(".yunBei").fadeIn();
        })
        $("#onadd").click(function(){
            $(".deparHover div.zTreeDemoBackground").fadeToggle();
            $(".inCharge").fadeOut();
        })

        $("#inCharge").click(function(){
            $(".deparHover div.zTreeDemoBackground").fadeOut();
            $(".inCharge").fadeToggle();
        })
        $("#deqparCancel").click(function(){
            $(".deparHover").fadeOut();
            $(".yunBei").fadeOut();
            location.reload();
        })
        $("#deparDetermine").click(function(){
            var depart_id = $('#depart_id').val();
            var pid = $('#parent_id').val();
            var depart_name = $('#depart_name').val();
            var leader_id = $('#inCharge').val();

            if (pid == 1) {
                var is_branch_department = $('input[name=is_branch_department]:checked').val();
                var data = {
                    depart_id:depart_id,
                    pid:pid,
                    depart_name:depart_name,
                    leader_id:leader_id,
                    is_branch_department:is_branch_department
                }
            }else{
                var data = {
                    depart_id:depart_id,
                    pid:pid,
                    depart_name:depart_name,
                    leader_id:leader_id
                }
            }
            // var postdata = $("#depart_form").serialize();
            var _url = $("#depart_form").attr('action');
            $.post(_url, data,function(data){
                if(data.code!=200){
                    shibai_alert(data.msg, 800);
                    return false;
                }
                btn_alert("成功", 800, function(){
                    window.location.reload();
                });
            },"json");
        });
        $("#search_btn").click(function(){
            var search_key = $("#search_key").val();
            var treeObj = $.fn.zTree.getZTreeObj("tree");
        });
    });

</script>
</head>
<include file="Public/common_header"/>
<div class="mian">
    <include file="Public/sidebar" menu="zhgl.zzjg"/>
    <div class="mianR">
        <h3 class="cha">组织架构</h3>
        <div class="department">
            <input type="button" value="添加部门" class="addTo">
            <div class="deparHover">
                <h4>添加部门</h4>
                <form  id="depart_form" action="/Depart/addDepart" method="post">
                    <input type="hidden" name="depart_id" id="depart_id"  value=""/>
                    <input type="hidden" name="pid" id="parent_id"/>
                    <label><input type="text" id="onadd" value="" readonly="">上级部门：</label>
                    <label><input type="text" name="depart_name" id="depart_name" class="responsible">部门名称：</label>
                    <label id="responsible_person" style="display:none">
                        <select name="leader" id="inCharge" style="float: right;width: 360px;">
                            <option value="0">请选择负责人</option>
                            <?php foreach($emps as $emp):?>
                            <option value="{$emp.emp_id}">{$emp.real_name}</option>
                            <?php endforeach;?>
                        </select>
                        部门负责人：
                    </label>
                    <div class="zTreeDemoBackground left">
                        <ul id="tree1" class="ztree">
                        </ul>
                    </div>
                </form>
                <label class="branch_title" style="display:none">
                    <em>是<input type="radio" name="is_branch_department" value="1" checked="checked"></em>
                    <em>否<input type="radio" name="is_branch_department" value="0"  ></em>
                    是否是分公司：
                    </label>
                <div class="deparBtn">
                    <input type="button" value="确定" class="deparQue" id="deparDetermine"><input type="button" value="取消" id="deqparCancel">
                </div>
            </div>
        </div>
        <div class="content_wrap">
            <!-- <div class="tree-search">
                <input type="text" id="search_key" placeholder="部门名称" />
                <input type="button" id="search_btn" value="搜索" />
            </div> -->
            <div class="zTreeDemoBackground left">
                <ul id="tree" class="ztree"></ul>
            </div>
        </div>
        <div class="treeRight">
            <h4><span>姓名</span><span>电话号码</span><span>是否是负责人</span></h4>
            <ul id="departMember">
                <!--<li><span>谭小斌</span><span>18692277486</span></li>-->
            </ul>
        </div>
    </div>
    <div class="clear"></div>
</div>
<include file="Public/foot" />